﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CircularLinkedList
{
    class Program
    {
        static void Main(string[] args)
        {
            CircularLinkedList ccl = new CircularLinkedList();
            ccl.Add(5);
            ccl.Add(7);
            ccl.Add(8);
            ccl.Add(12);
            ccl.Display();
            Console.ReadLine();

            // my comment here ok!
        }
    }

    class CircularLinkedList
    {
        public Node Head = null;
        public void Add(int val)
        {
            Node node = Head;
            bool traversed = false;
            while (node != null && (node.Next != Head || !traversed))
            {
                node = node.Next;
                traversed = true;
            }

            Node newNode = new Node(val);
            if (Head == null)
                Head = newNode;
            else
                node.Next = newNode;
            newNode.Next = Head;
        }
        public void Remove(int val)
        {
            Node node = Head;
            bool traversed = false;
            while (node != null && (node.Next != Head || !traversed))
            {
                node = node.Next;
                traversed = true;
            }

            Node newNode = new Node(val);
            if (Head == null)
                Head = newNode;
            else
                node.Next = newNode;
            newNode.Next = Head;
        }
        public void Display()
        {
            Node node = Head;
            bool traversed = false;
            while (node != Head || !traversed)
            {
                if (node != null)
                {
                    Console.Write(node.Data + " ");
                    node = node.Next;
                }
                traversed = true;
            }
        }
    }

    class Node
    {
        public Node Next;
        public int Data;
        public Node(int val)
        {
            this.Data = val;
        }
    }
}
